﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Gtkj.CMS.Common
{
    public class RESTfulClient
    {
        public string Get(string Url)
        {
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            try
            {
                var bytes = wc.DownloadData(Url);
                return Encoding.UTF8.GetString(bytes);
            }
            catch
            {
                return null;
            }
        }

        public T Get<T>(string Url)
        {
            WebClient wc = new WebClient();
            wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
            wc.Encoding = Encoding.UTF8;
            try
            {
                var bytes = wc.DownloadData(Url);
                return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(bytes));
            }
            catch
            {
                return default(T);
            }
        }

        public async Task<T> GetAsync<T>(string Url)
        {
            return await Task.Run(() =>
            {
                return Get<T>(Url);
            });
        }

        public string Post<T>(string Url, T Param)
        {
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
            StringBuilder sb = new StringBuilder();
            foreach (var attr in Param.GetType().GetProperties())
            {
                sb.Append("&")
                    .Append(attr.Name)
                    .Append("=")
                    .Append(attr.GetValue(Param).ToString());
            }
            string data = "";
            if (sb.Length > 0)
            {
                data = sb.ToString().Substring(1);
            }
            else
            {
                data = sb.ToString();
            }
            var rtnBytes = wc.UploadData(Url, "POST", Encoding.UTF8.GetBytes(data));
            string rtnStr = Encoding.UTF8.GetString(rtnBytes);
            return rtnStr;
        }

        public async Task<string> PostAsync<T>(string Url, T Param)
        {
            return await Task.Run(() =>
            {
                return Post(Url, Param);
            });
        }

        public T1 Post<T1, T2>(string Url, T2 Param)
        {
            WebClient wc = new WebClient();
            wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
            wc.Encoding = Encoding.UTF8;

            string ser = JsonConvert.SerializeObject(Param);
            var bytes = Encoding.UTF8.GetBytes(ser);
            try
            {
                return JsonConvert.DeserializeObject<T1>(Encoding.UTF8.GetString(wc.UploadData(Url, bytes)));
            }
            catch
            {
                return default(T1);
            }
        }


        public async Task<T1> PostAsync<T1, T2>(string Url, T2 Param)
        {
            return await Task.Run(() =>
            {
                return Post<T1, T2>(Url, Param);
            });
        }
    }
}